<?php


namespace TbfProtocol;


use TbfProtocol\ProtocolProvider;
use TbfProtocol\Response;

class ApiProtocol implements ProtocolProvider{
    private static $obj = null;
    static function obj(){
        if (self::$obj===null){
            self::$obj = new static();
        }
        return self::$obj;
    }
    private function __construct(){}
    /**
     * 请求开始的时候的回调(调用使用协议的时候)
     * @return void
     */
    function processRequest(){
        \TbfInput::mustPost();
    }

    /**
     * 处理返回数据
     * @param Response $resp
     * @return void
     */
    function processResponse(Response $resp){
        $data = $resp->toArray();
        if ($resp->type===Response::TYPE_CLIENT_ERROR ||
            $resp->type===Response::TYPE_INNER_ERROR){
            $data['type'] = 'error';
        }
        echo json_encode($data);
        return;
    }
}